# Maintainer: Philipp Smirnov https://github.com/sad-poet

_realname=fswatch
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=1.18.2
pkgrel=1
_branch=master
pkgdesc="A cross-platform file change monitor with multiple backends: Apple OS X File System Events API, *BSD kqueue, Solaris/Illumos File Events Notification, Linux inotify, Microsoft Windows and a stat()-based backend. (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://emcrisostomo.github.io/fswatch/"
msys2_repository_url="https://github.com/emcrisostomo/fswatch"
msys2_references=(
  'aur: fswatch'
  'gentoo: sys-fs/fswatch'
)
license=('spdx:Apache-2.0 OR GPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gettext-runtime")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-gettext-tools")
source=("https://github.com/emcrisostomo/fswatch/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        '001-Fix-for-MinGW-w64.patch')
sha256sums=('698f21fe5489311dabe9e90463fb9d40796780abac8d207b857e86ade7345d86'
            'e3c26824d9faadc7164765b9ed87fd65005b02a5ee50f35e39984bef29711d37')
noextract=("${_realname}-${pkgver}.tar.gz")

prepare() {
  echo "Extracting ${_realname}-${pkgver}.tar.gz ..."
  tar -xzf ${_realname}-${pkgver}.tar.gz || true

  cd "${srcdir}/${_realname}-${pkgver}"

  # https://github.com/emcrisostomo/fswatch/issues/214
  patch -p1 -i ${srcdir}/001-Fix-for-MinGW-w64.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=ON \
    "${_extra_config[@]}" \
    -B build-${MSYSTEM} \
    -S ${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build "${srcdir}/build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING \
    ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
}
